package com.cheng.operation.impl;

import com.cheng.common.exception.CommonException;
import com.cheng.common.utils.CodeUtil;
import com.cheng.common.utils.RespUtils;
import com.cheng.common.utils.StringUtil;
import com.cheng.data.entity.CompanyInfoEntity;
import com.cheng.data.vo.CompanyInfoVO;
import com.cheng.data.vo.ReqPageInfoVO;
import com.cheng.data.vo.RespPageInfoVO;
import com.cheng.data.vo.StatisticsVO;
import com.cheng.operation.ArchiveTypeInfOperation;
import com.cheng.operation.CompanyInfoOperation;
import com.cheng.operation.EntpTypeInfOperation;
import com.cheng.operation.GvmtTypeInfOperation;
import com.cheng.service.CompanyInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: ChengJW
 * @date: 2021/11/29 22:37
 * @version: 1.0
 * @description: 企业信息业务逻辑处理
 */
@Service
public class CompanyInfoOperationImpl implements CompanyInfoOperation {

    private final static Logger log = LoggerFactory.getLogger(CompanyInfoOperationImpl.class);

    @Autowired
    private CompanyInfoService companyInfoService;

    @Autowired
    private EntpTypeInfOperation entpTypeInfOperation;

    @Autowired
    private ArchiveTypeInfOperation archiveTypeInfOperation;

    @Autowired
    private GvmtTypeInfOperation gvmtTypeInfOperation;

    /**
    * @author: ChengJW
    * @params: [vo]
    * @return: com.cheng.data.vo.RespPageInfoVO
    * @date: 2021/11/29
    * @description: 查询企业信息
    */
    @Override
    public RespPageInfoVO queryCompanyInfoVOList(ReqPageInfoVO vo) throws CommonException {

        if (null == vo){
            log.error("查询条件信息为空，无法进行下一步操作");
            throw new CommonException("CJW202111010054","CompanyInfoOperationImpl","queryCompanyInfoVOList");
        }
        Map<String,Object> map = new HashMap<>();
        map.put(CodeUtil.SRCH_REQ_FIR,vo.getSrchReqFir());
        if (StringUtil.isNotNull(String.valueOf(vo.getQryReqFir()))){
            map.put(CodeUtil.QRY_REQ_FIR,"%"+vo.getQryReqFir()+"%");
        }
        if (StringUtil.isNotNull(vo.getStDt())){
            map.put("stDt",vo.getStDt()+" 00:00:00");
        }
        if (StringUtil.isNotNull(vo.getLmDt())){
            map.put("lmDt",vo.getLmDt()+" 23:59:59");
        }
        map.put(CodeUtil.USER_CODE,vo.getUserCode());

        PageInfo<CompanyInfoVO> pageInfo = PageHelper.startPage(vo.getPageNum(),vo.getPageSize()).doSelectPageInfo(() -> companyInfoService.queryCompanyVOList(map));
        if (pageInfo.getList() != null && pageInfo.getList().size() > 0){
            for (CompanyInfoVO companyInfoVO : pageInfo.getList()){
                companyInfoVO.setCiType(entpTypeInfOperation.queryEntpTypeInfByCode(companyInfoVO.getCiType()));
                companyInfoVO.setCiArchTpye(archiveTypeInfOperation.queryArchiveTypeInfNmByCode(companyInfoVO.getCiArchTpye()));
            }
        }
        return RespUtils.respPageInfoVO(pageInfo);
    }

    /**
    * @author: ChengJW
    * @params: [vo]
    * @return: com.cheng.data.vo.RespPageInfoVO
    * @date: 2021/12/15
    * @description: 迁出企业列表查询
    */
    @Override
    public RespPageInfoVO queryArchivesMoveList(ReqPageInfoVO vo) {
        if (null == vo){
            log.error("查询条件信息为空，无法进行下一步操作");
            throw new CommonException("CJW202111010054","CompanyInfoOperationImpl","queryCompanyInfoVOList");
        }
        Map<String,Object> map = new HashMap<>();
        map.put(CodeUtil.SRCH_REQ_FIR,vo.getSrchReqFir());
        if (StringUtil.isNotNull(String.valueOf(vo.getQryReqFir()))){
            map.put(CodeUtil.QRY_REQ_FIR,"%"+vo.getQryReqFir()+"%");
        }
        map.put(CodeUtil.USER_CODE,vo.getUserCode());
        PageInfo<CompanyInfoVO> pageInfo = PageHelper.startPage(vo.getPageNum(),vo.getPageSize()).doSelectPageInfo(() -> companyInfoService.queryArchivesMoveOutList(map));
        return RespUtils.respPageInfoVO(pageInfo);
    }

    /**
    * @author: ChengJW
    * @params: [pageInfoVO]
    * @return: com.cheng.data.vo.RespPageInfoVO
    * @date: 2021/12/17
    * @description: 根据条件查询企业信息
    */
    @Override
    public RespPageInfoVO queryCompanyInfoList(ReqPageInfoVO pageInfoVO) {

        if(pageInfoVO == null){
            log.error("查询条件信息为空，无法进行下一步操作");
            throw new CommonException("CJW202111010054","CompanyInfoOperationImpl","queryCompanyInfoList");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("ci_Credit_Code",pageInfoVO.getQryReqFir());
        map.put("ci_Mge_Code",pageInfoVO.getUserCode());
        PageInfo<CompanyInfoEntity> pageInfo = PageHelper.startPage(pageInfoVO.getPageNum(),pageInfoVO.getPageSize()).doSelectPageInfo(() -> companyInfoService.queryCompanyInfoList(map));
        List<CompanyInfoEntity> companyInfoVOList = pageInfo.getList();
        List<CompanyInfoEntity> list = new ArrayList<>();
        if (null != companyInfoVOList && companyInfoVOList.size() > 0){
            for (CompanyInfoEntity entity : companyInfoVOList){
                entity.setCiArchTpye(archiveTypeInfOperation.queryArchiveTypeInfNmByCode(entity.getCiArchTpye()));
                list.add(entity);
            }
        }
        pageInfo.setList(list);
        return RespUtils.respPageInfoVO(pageInfo);
    }

    /**
    * @author: ChengJW
    * @params: [pageVO]
    * @return: com.cheng.data.vo.RespPageInfoVO
    * @date: 2021/12/19
    * @description: 根据企业类型分类时，统计企业信息
    */
    @Override
    public RespPageInfoVO queryStatisticsVOList(ReqPageInfoVO pageVO) throws CommonException {

        if (pageVO == null || pageVO.getSrchReqFir() == null){
            log.error("查询条件信息为空，无法进行下一步操作");
            throw new CommonException("CJW202111010054","CompanyInfoOperationImpl","queryStatisticsVOList");
        }

        //对查询结果进行分页
        Map<String,Object> map = new HashMap<>();
        map.put(CodeUtil.STATISTIC_CODE,pageVO.getSrchReqFir());
        //开始日期为当天的第一秒
        if (StringUtil.isNotNull(pageVO.getStDt())){
            map.put("stDt",pageVO.getStDt()+" 00:00:00");
        }
        if (StringUtil.isNotNull(pageVO.getLmDt())){
            map.put("lmDt",pageVO.getLmDt()+" 23:59:59");
        }
        PageInfo<StatisticsVO> pageInfo = PageHelper.startPage(pageVO.getPageNum(),pageVO.getPageSize()).doSelectPageInfo(() -> companyInfoService.queryStatisticsVOlist(map));
        List<StatisticsVO> list = new ArrayList<>();
        if (pageInfo.getList() != null && pageInfo.getList().size() > 0){
            for (StatisticsVO vo : pageInfo.getList()){
                vo.setMgeNm(gvmtTypeInfOperation.queryGvmtTypeInfNmByCode(vo.getMgeNm()));
                list.add(vo);
            }
        }
        pageInfo.setList(list);
        return RespUtils.respPageInfoVO(pageInfo);
    }

    /**
     * @author: ChengJW
     * @params: [pageInfoVO]
     * @return: com.cheng.data.vo.RespPageInfoVO
     * @date: 2021/12/20
     * @description: 档案类型分类时，统计档案信息
     */
    @Override
    public RespPageInfoVO queryStatisticsArchVOList(ReqPageInfoVO pageVO) throws CommonException {
        if (pageVO == null || pageVO.getSrchReqFir() == null){
            log.error("查询条件信息为空，无法进行下一步操作");
            throw new CommonException("CJW202111010054","CompanyInfoOperationImpl","queryStatisticsVOList");
        }

        //对查询结果进行分页
        Map<String,Object> map = new HashMap<>();
        map.put(CodeUtil.STATISTIC_CODE,pageVO.getSrchReqFir());
        //开始日期为当天的第一秒
        if (StringUtil.isNotNull(pageVO.getStDt())){
            map.put("stDt",pageVO.getStDt()+" 00:00:00");
        }
        if (StringUtil.isNotNull(pageVO.getLmDt())){
            map.put("lmDt",pageVO.getLmDt()+" 23:59:59");
        }
        PageInfo<StatisticsVO> pageInfo = PageHelper.startPage(pageVO.getPageNum(),pageVO.getPageSize()).doSelectPageInfo(() -> companyInfoService.queryStatisticsArchVOList(map));
        List<StatisticsVO> list = new ArrayList<>();
        if (pageInfo.getList() != null && pageInfo.getList().size() > 0){
            for (StatisticsVO vo : pageInfo.getList()){
                vo.setMgeNm(gvmtTypeInfOperation.queryGvmtTypeInfNmByCode(vo.getMgeCode()));
                list.add(vo);
            }
        }
        pageInfo.setList(list);
        return RespUtils.respPageInfoVO(pageInfo);
    }
}
